package com.bank.dao;

import java.util.List;
import java.util.Map;


import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.bank.entity.Account;

@Service
public class AccountDao  extends BaseDao{
	
	Logger logger = Logger.getLogger(AccountDao.class);
	
	@Autowired
	SessionFactory  sessionFactory;
	
	
	
	public void save(Account account){
		logger.debug("开始插入数据account_list");
		Session session = sessionFactory.getCurrentSession();
		session.save(account);
		session.flush();
		logger.debug("插入完毕数据account_list");
	}
	
	/**
	 * 更改账户状态
	 * **/
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	public void updateAccountStatus(String number){
		Account account = this.getAccountByNumber(number);
		account.setAccount_status("0");//0  销户
		Session session = sessionFactory.getCurrentSession();
		session.update(account);
		session.flush();
	}
	
	/**
	 * 根据账号获取账户信息
	 * @param number  账号
	 * @return Account  账号信息
	 * **/
	
	public Account getAccountByNumber(String number){
		GetGeneralBean<Account> getGeneralBean = new GetGeneralBean<Account>();
		String sql = "from  Account where number='"+number+"'";
		return  getGeneralBean.getGenerlBean(sql);
	}
	@SuppressWarnings("unchecked") 
	public List<Object> getAcountList(Map<String, Object> params){
		String sql = "from Account where 1=1 ";
		Integer offsetInt  = (Integer)params.get("offset");
		Integer limitInt = (Integer)params.get("limit");
		String customer_number = (String)params.get("customer_number");
		String number  = (String)params.get("number");
		String senior_oir = (String)params.get("senior_oir");
		String customer_name = (String)params.get("customer_name");
		logger.debug("offsetInt"+offsetInt);
		logger.debug("limitInt"+limitInt);
		logger.debug("customer_number"+customer_number);
		logger.debug("customer_name"+customer_name);
		if(customer_name!=null && customer_name.equals("")==false){
			sql = sql+" and customer_number ='"+customer_number+"'";
		}
		if(number != null && number.equals("")==false){
			sql = sql+" and  number='"+number+"'";
		}
		if(senior_oir != null && senior_oir.equals("")==false){
			sql = sql+"  and senior_oir='"+senior_oir+"'";
		}
		if(customer_name != null && customer_name.equals("")==false){
			sql = sql + " and customer_name='"+customer_name+"'";
		}
		Session  session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(sql);
		query.setFirstResult(offsetInt);
		query.setMaxResults(limitInt);
		List<Object> accountList = query.list();	
		return accountList;
	}
	public int getAccountListCount(Map<String, Object> params){
		String sqlCount = "select count(*) from Account where 1=1 ";
		String customer_number = (String)params.get("customer_number");
		String number  = (String)params.get("number");
		String senior_oir = (String)params.get("seniosr_oir");
		String customer_name = (String)params.get("customer_name");
		if(customer_name!=null && customer_name.equals("")==false){
			sqlCount = sqlCount+" and  customer_number ='"+customer_number+"'";
		}
		if(number != null && number.equals("")==false){
			sqlCount = sqlCount+" and number='"+number+"'";
		}
		if(senior_oir != null && senior_oir.equals("")==false){
			sqlCount = sqlCount+" and senior_oir='"+senior_oir+"'";
		}
		if(customer_name != null && customer_name.equals("")==false){
			sqlCount = sqlCount + " and customer_name='"+customer_name+"'";
		}
		Session  session = sessionFactory.getCurrentSession();
		int totalRecord = Integer.parseInt(session.createQuery(sqlCount).uniqueResult().toString());
		return totalRecord;

	}
	
	/*更新账户余额*/
	public void updateAccountBalance(Account account){
		Session session = sessionFactory.getCurrentSession();
		session.update(account);
		session.flush();
	}
	
	/** 查询内部账
	 * @param currency  货币代号
	 * 		  accounting_ori   帐务机构
	 * 		  account_subject  科目号
	 * @return  内部账信息
	 * */
	public  Account getAccountByParams(String currency, String accounting_ori, String account_subject){
		GetGeneralBean<Account> getGeneralBean = new GetGeneralBean<Account>();
		String  sql = "from Account where currency='"+currency+"' and  accounting_ori='"+accounting_ori+"' and account_subject='"+account_subject+"'";
		return getGeneralBean.getGenerlBean(sql);
	}
}
